package com.esri.android.map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.popup.PopupLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.internal.util.j;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.Feature;
import com.github.mikephil.charting.utils.Utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes.dex */
public abstract class DynamicLayer extends Layer implements PopupLayer {
    static float a = 213.0f;
    private static final long s = 1;
    double b;
    double c;
    double d;
    double e;
    a g;
    private double t;
    private boolean u;
    private ImageFilter v;
    protected SpatialReference[] supportedSRs = null;
    Future<?> f = null;

    protected abstract byte[] getImage(int i, int i2, Envelope envelope) throws Exception;

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupAllowGeometryUpdate(Feature feature) {
        return false;
    }

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupDeletable(Feature feature) {
        return false;
    }

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupEditable(Feature feature) {
        return false;
    }

    native long nativeCreateLayer();

    native float nativeGetBrightness(long j);

    native float nativeGetContrast(long j);

    native float nativeGetGamma(long j);

    native boolean nativeInitialize(long j, int i, String str, double d, double d2, double d3, double d4, int[] iArr, String[] strArr, boolean z);

    native void nativeRefresh(long j);

    native void nativeSetBrightness(long j, float f);

    native void nativeSetContrast(long j, float f);

    native void nativeSetGamma(long j, float f);

    native void nativeSetImage(long j, double d, double d2, double d3, double d4, byte[] bArr, int i);

    /* loaded from: classes.dex */
    class a implements CallbackListener<byte[]> {
        double a;
        double b;
        double c;
        private double e;

        a(double d, double d2, double d3, double d4) {
            this.e = d;
            this.a = d2;
            this.b = d3;
            this.c = d4;
        }

        @Override // com.esri.core.map.CallbackListener
        public void onError(Throwable th) {
            if ((th instanceof InterruptedException) || (th instanceof InterruptedIOException)) {
                return;
            }
            Log.e(com.esri.core.internal.a.a, "ImageCallback.onError", th);
        }

        @Override // com.esri.core.map.CallbackListener
        /* renamed from: a */
        public void onCallback(byte[] bArr) {
            byte[] bArr2 = bArr;
            if (!Thread.currentThread().isInterrupted() && this.e == DynamicLayer.this.b && this.b == DynamicLayer.this.d && this.a == DynamicLayer.this.c && this.c == DynamicLayer.this.e) {
                if (DynamicLayer.this.v != null && bArr2 != null) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    options.inMutable = true;
                    Bitmap applyFilter = DynamicLayer.this.v.applyFilter(BitmapFactory.decodeByteArray(bArr2, 0, bArr2.length, options));
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    applyFilter.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    bArr2 = byteArrayOutputStream.toByteArray();
                    try {
                        byteArrayOutputStream.close();
                    } catch (IOException unused) {
                    }
                }
                byte[] bArr3 = bArr2;
                int length = bArr3 == null ? 0 : bArr3.length;
                if (DynamicLayer.this.nativeHandle != 0) {
                    DynamicLayer dynamicLayer = DynamicLayer.this;
                    dynamicLayer.nativeSetImage(dynamicLayer.getID(), this.e, this.a, this.b, this.c, bArr3, length);
                }
            }
        }
    }

    /* loaded from: classes.dex */
    class b<V> implements TaskListener<V> {
        private final CallbackListener<V> b;

        public b(String str, CallbackListener<V> callbackListener) {
            this.b = callbackListener;
        }

        @Override // com.esri.core.internal.tasks.TaskListener
        public void onError(Throwable th) {
            this.b.onError(th);
            DynamicLayer.this.f = null;
        }

        @Override // com.esri.core.internal.tasks.TaskListener
        public void onCompletion(short s, V v) {
            if (s == 1) {
                this.b.onCallback(v);
            }
            DynamicLayer.this.f = null;
        }
    }

    protected void getImageAsych(final int i, final int i2, final Envelope envelope, final CallbackListener<byte[]> callbackListener) {
        final String url = getUrl();
        Future<?> future = this.f;
        if (future != null) {
            future.cancel(true);
        }
        try {
            this.f = getPoolExecutor().submit(new Runnable() { // from class: com.esri.android.map.DynamicLayer.1
                @Override // java.lang.Runnable
                public void run() {
                    b bVar = new b(url, callbackListener);
                    try {
                        bVar.onCompletion((short) 1, DynamicLayer.this.getImage(i, i2, envelope));
                    } catch (Throwable th) {
                        bVar.onError(th);
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public void setImageFilter(ImageFilter imageFilter) {
        this.v = imageFilter;
    }

    @Override // com.esri.android.map.Layer
    public void recycle() {
        cancelPendingTasks();
        super.recycle();
    }

    public void cancelPendingTasks() {
        try {
            Future<?> future = this.f;
            if (future != null) {
                future.cancel(true);
            }
            this.f = null;
        } catch (NullPointerException unused) {
        }
    }

    public DynamicLayer(String str) {
        setUrl(str);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public long create() {
        return nativeCreateLayer();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void initLayer() {
        int[] iArr;
        String[] strArr;
        DynamicLayer dynamicLayer;
        SpatialReference defaultSpatialReference = getDefaultSpatialReference();
        if (defaultSpatialReference != null) {
            int id = defaultSpatialReference.getID();
            String text = defaultSpatialReference.getText();
            Envelope fullExtent = getFullExtent();
            int i = 0;
            boolean z = (this.m == null || !(defaultSpatialReference.isWGS84() || defaultSpatialReference.isAnyWebMercator())) ? false : this.m.k;
            SpatialReference[] spatialReferenceArr = this.supportedSRs;
            if (spatialReferenceArr == null || spatialReferenceArr.length <= 0) {
                iArr = null;
                strArr = null;
            } else {
                int[] iArr2 = new int[spatialReferenceArr.length];
                String[] strArr2 = new String[spatialReferenceArr.length];
                while (true) {
                    SpatialReference[] spatialReferenceArr2 = this.supportedSRs;
                    if (i >= spatialReferenceArr2.length) {
                        break;
                    }
                    SpatialReference spatialReference = spatialReferenceArr2[i];
                    try {
                        if (spatialReference.getID() > -1) {
                            iArr2[i] = spatialReference.getID();
                        } else {
                            strArr2[i] = spatialReference.getText();
                        }
                    } catch (Exception unused) {
                    }
                    i++;
                }
                strArr = strArr2;
                iArr = iArr2;
            }
            if (fullExtent == null || fullExtent.isEmpty()) {
                dynamicLayer = this;
            } else if (nativeInitialize(this.nativeHandle, id, text, fullExtent.getXMin(), fullExtent.getYMin(), fullExtent.getXMax(), fullExtent.getYMax(), iArr, strArr, z)) {
                changeStatus(OnStatusChangedListener.STATUS.INITIALIZED);
                return;
            } else {
                dynamicLayer = this;
            }
            dynamicLayer.changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_DYNAMIC_LAYER));
            return;
        }
        changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_DYNAMIC_LAYER));
    }

    public float getBrightness() {
        if (this.nativeHandle != 0) {
            return nativeGetBrightness(this.nativeHandle);
        }
        return Float.NaN;
    }

    public void setBrightness(float f) {
        if (this.nativeHandle != 0) {
            nativeSetBrightness(this.nativeHandle, f);
        }
    }

    public float getContrast() {
        if (this.nativeHandle != 0) {
            return nativeGetContrast(this.nativeHandle);
        }
        return Float.NaN;
    }

    public void setContrast(float f) {
        if (this.nativeHandle != 0) {
            nativeSetContrast(this.nativeHandle, f);
        }
    }

    public float getGamma() {
        if (this.nativeHandle != 0) {
            return nativeGetGamma(this.nativeHandle);
        }
        return Float.NaN;
    }

    public void setGamma(float f) {
        if (this.nativeHandle != 0) {
            nativeSetGamma(this.nativeHandle, f);
        }
    }

    protected void requestImage(int i, int i2, double d, double d2, double d3, double d4) {
        this.b = d;
        this.c = d2;
        this.d = d3;
        this.e = d4;
        this.g = new a(d, d2, d3, d4);
        getImageAsych(i, i2, a(d, d2, d3, d4), this.g);
    }

    public void refresh() {
        if (this.nativeHandle != 0) {
            nativeRefresh(this.nativeHandle);
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public boolean isWrapAroundEnabled() {
        if (this.m == null) {
            return false;
        }
        return this.m.s();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public boolean isCrossDateline() {
        return this.u;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public double getCenterMeridan() {
        return this.t;
    }

    Envelope a(double d, double d2, double d3, double d4) {
        Envelope envelope = new Envelope(d, d2, d3, d4);
        if (isWrapAroundEnabled()) {
            if (getSpatialReference().isWGS84()) {
                return a(envelope, j.b, false);
            }
            if (getSpatialReference().isAnyWebMercator()) {
                return a(envelope, j.c, true);
            }
        }
        return envelope;
    }

    private Envelope a(Envelope envelope, double d, boolean z) {
        double xMin = envelope.getXMin();
        double xMax = envelope.getXMax();
        double d2 = -d;
        boolean z2 = xMin >= d2 && xMin <= d;
        boolean z3 = xMax >= d2 && xMax <= d;
        this.u = false;
        j.a(envelope, d2, d, getSpatialReference());
        if (z2 && z3) {
            return envelope;
        }
        Point center = envelope.getCenter();
        boolean a2 = j.a(envelope.getXMin(), envelope.getXMax(), getSpatialReference());
        this.u = a2;
        if (a2) {
            envelope.offset(envelope.getCenterX() * (-1.0d), Utils.DOUBLE_EPSILON);
            if (z) {
                this.t = ((Point) GeometryEngine.project(center, getSpatialReference(), SpatialReference.create(SpatialReference.WKID_WGS84))).getX();
            } else {
                this.t = center.getX();
            }
            return envelope;
        }
        return envelope;
    }

    @Override // com.esri.android.map.Layer
    public void setRenderNativeResolution(boolean z) {
        super.setRenderNativeResolution(z);
        if (getDpi() != 0.0f) {
            float p = this.m.p();
            if (z) {
                setRenderPixelsPerInch(p);
                return;
            }
            float f = a;
            if (p >= f) {
                setRenderPixelsPerInch(f);
            } else {
                setRenderPixelsPerInch(p);
            }
        }
    }
}
